/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BeanView;

import Dao.DaoFirstName;
import HibernateUtil.HibernateUtil;
import Pojo.Employees;
import Pojo.Firstname;
import java.util.Arrays;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author knyazev_o
 */
@ManagedBean
@ViewScoped
public class MbVFirstName implements java.io.Serializable{
    private Session session;
    private Transaction transaction;
    private Firstname selectedFirstName;
    private Firstname firstName;
    private List<Firstname> listFirstName;
    private List<Firstname> listFirstNameFiltered;
    private String newName;
    
    
    public MbVFirstName(){
        this.firstName=new Firstname();
        this.firstName.setId(0L);
        this.newName = "";
    }
    
    public void registration(){
        this.session=null;
        this.transaction=null;
        
        try
        {   
            if(this.newName.trim().equals(""))
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Укажите имя"));
                return;
            }
            DaoFirstName daoFirstName=new DaoFirstName();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            String query = "from Firstname f where lower(f.name)=lower(?)";
            List<Object> param = Arrays.asList((Object)this.newName);
            if(daoFirstName.selectFirstName(this.session, query, param)!=null)
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Имя уже зарегистрировано в системе"));
                return;
            }
            this.firstName.setName(newName);           
            daoFirstName.register(this.session, this.firstName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Регистрация прошла успешно"));
            this.firstName=new Firstname();
            this.firstName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public List<Firstname> getAll(){
        this.session=null;
        this.transaction=null;
        
        try
        {
            DaoFirstName daoFirstName=new DaoFirstName();            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();            
            this.listFirstName=daoFirstName.getAll(this.session);            
            this.transaction.commit();            
            return this.listFirstName;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }        
    }
    
    public Firstname getId(String id){
        this.session=null;
        this.transaction=null;        
        try
        {
            DaoFirstName daoFirstName=new DaoFirstName();             
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();  
            String query = "from Firstname fn where fn.id = ?";
            List<Object> param = Arrays.asList((Object)Long.parseLong(id));
            this.firstName=daoFirstName.selectFirstName(this.session, query, param);           
            this.transaction.commit();            
            return this.firstName;
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));            
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public void remove(){
        this.session=null;
        this.transaction=null;        
        try
        {            
            DaoFirstName daoFirstName=new DaoFirstName();
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();                     
            daoFirstName.delete(this.session, this.firstName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Организация удалена"));
            this.firstName=new Firstname();
            this.firstName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public void update(){
        this.session=null;
        this.transaction=null;        
        try
        {            
            DaoFirstName daoFirstName=new DaoFirstName();
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=session.beginTransaction();
            String query = "from Firstname f where f.id <> ? and f.name = ?";
            List<Object> param = Arrays.asList((Object)this.firstName.getId(), 
                                (Object)this.firstName.getName());
            if(daoFirstName.selectFirstName(this.session, query, param)!=null)
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка:", "Организация уже зарегистрирована в системе"));
                return;
            }           
            daoFirstName.update(this.session, this.firstName);            
            this.transaction.commit();            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Успех:", "Регистрация прошла успешно"));
            this.firstName=new Firstname();
            this.firstName.setId(0L);
        }
        catch(Exception ex)
        {
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public String cancel(){
        this.firstName=new Firstname();
        this.firstName.setId(0L);
        return null;
    }
        
    public void setFirstNameParent(){
        Object parent=FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("parent");
        if(parent instanceof Employees){
            Employees employees=(Employees)parent;
            employees.setFirstname(this.firstName);
        }
        this.firstName=new Firstname();
        this.firstName.setId(0L);
    }
    
    public Firstname getFirstName() {
        return firstName;
    }

    public void setFirstName(Firstname firstName) {
        this.firstName = firstName;
    }

    public Firstname getSelectedFirstName() {
        return selectedFirstName;
    }

    public void setSelectedFirstName(Firstname selectedFirstName) {
        this.selectedFirstName = selectedFirstName;
    }

    public List<Firstname> getListFirstName() {
        return listFirstName;
    }

    public void setListFirstName(List<Firstname> listFirstName) {
        this.listFirstName = listFirstName;
    }

    public List<Firstname> getListFirstNameFiltered() {
        return listFirstNameFiltered;
    }

    public void setListFirstNameFiltered(List<Firstname> listFirstNameFiltered) {
        this.listFirstNameFiltered = listFirstNameFiltered;
    }
    
    public String getNewName() {
        return this.newName;
    }

    public void setNewName(String newName) {
        this.newName = newName;
    } 
    
}
